package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.domain.Result;
import com.itheima.domain.User1;
import com.itheima.service.UserService;
import com.itheima.utils.SendSmsUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
//
//    @RequestMapping("/hello")
//    public String sayHello(Model model){
//
//        System.out.println("=======>SpringBoot整合Freemarker测试案例");
//
//        model.addAttribute("title","SpringBoot整合Freemarker");
//        model.addAttribute("message","HELLO Freemarker");
//        return "hello";
//    }
//
//    @RequestMapping("/hello2")
//    public String sayHello2(Map map){
//
//        System.out.println("=======>SpringBoot整合Freemarker测试案例");
//
//        map.put("title","SpringBoot整合Freemarker2");
//        map.put("message","HELLO Freemarker2");
//        return "hello";
//    }

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/getCheckCode")
    public Result getCheckCode(String telephone){
        try{
            //1.获取手机号
            if(telephone==null || "".equals(telephone)){
                return new Result(false,"手机号码不正确");
            }
            //2.随机生成6位验证码
            String checkCode = RandomStringUtils.randomNumeric(6);
            //3.将验证码存入Redis中
            redisTemplate.boundValueOps(telephone+"001").set(checkCode);
            //4.发送短信给用户
            Map map = new HashMap();
            map.put("code",checkCode);
            String message = JSONObject.toJSONString(map);
            SendSmsUtils.sendSms(telephone,message);
            System.out.println("=====>"+checkCode);
            //5.将生产的验证码返回给用户
            return new Result(true,checkCode);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"获取验证码失败");
        }
    }

    @PostMapping("/register")
    public Result register(User1 user, String checkCode){
        try{
            //1.获取用户输入的验证码,判断验证码是否有值
            if(checkCode==null || "".equals(checkCode)){
                return new Result(false,"验证码不能为空");
            }
            //2.从Redis中获取手机号对应的验证码
            String telephone = user.getTelephone();
            if(telephone==null || "".equals(telephone)){
                return new Result(false,"手机号码不能为空");
            }
            //格式验证略
            String redisCode = redisTemplate.opsForValue().get(telephone+"001").toString();
            //3.对比两个验证码是否一致
            if(!checkCode.equals(redisCode)){
                return new Result(false,"验证码输入错误");
            }
            //4.完成注册信息
            userService.addUser(user);
            //5.清空redis数据
            redisTemplate.delete(telephone+"001");
            return new Result(true,"注册成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"注册失败");
        }
    }
}
